/* 会员 */
import { getAccessMember } from '../api/member'

class Member {
  constructor() {
    this.id = null // ID(唯一)
    this.nickName = '' // 昵称
    this.avatarUrl = '' // 头像连接
    this.gender = 0 // 性别
    this.country = '' // 国家
    this.province = '' // 省
    this.city = '' // 市
    this.openid = null // Openid(唯一)
    this.phone = null // 手机号(唯一)
    this.level = 1 // 会员等级数
    this.gold = 0 // 会员积分数
    this.cardSum = 0 // 会员卡券数
  }

  async refreshProfile() {
    const { userInfo } = await wx.getUserProfile({
      desc: '会员信息展示',
    })
    // TODO 远程同步会员信息
    console.log('userInfo', userInfo)
    for (const key in userInfo) {
      if (Object.hasOwnProperty.call(this, key)) {
        this[key] = userInfo[key]
      }
    }
  }
}

// 以make为前缀的函数为工厂函数，工厂函数一般与类定义位于相同包内
export function makeMember(context) {
  return new Member()
}

export async function makeAccessMember() {
  const { data } = await getAccessMember()
  const member = new Member()
  member.id = data.id
  member.nickName = data.nickname
  member.avatarUrl = data.avatar_url
  member.gender = data.gender
  member.country = data.country
  member.province = data.province
  member.city = data.city
  member.openid = data.openid
  member.phone = data.phone
  member.level = data.level
  member.gold = data.gold
  member.cardSum = data.card_sum
  return member
}
